import java.io.Serializable;
import java.net.InetAddress;

import edu.ucla.cs.typecast.net.EndPoint;
import edu.ucla.cs.typecast.net.NetUtil;
import edu.ucla.cs.typecast.rmi.Fact;
import edu.ucla.cs.typecast.rmi.Future;
import edu.ucla.cs.typecast.rmi.TypeCastClient;
import edu.ucla.cs.typecast.rmi.TypeCastServer;
import edu.ucla.cs.typecast.rmi.server.TypeCastObjectManager;


public class PingServer implements PingService, Serializable {
    public void ping(InetAddress source, String value) { 
            System.out.println(this.getClass().getName() + ": " + source + " ping " + value ); 
    }
    public void pingExpectPong(EndPoint pongEndPoint, String value) { 
    	try { 
	    	System.out.println(this.getClass().getName() + ": " +  pongEndPoint.getAddress() + " ping " + value 
	    			+ ", return pong to port " + pongEndPoint.getPort()); 
	    	TypeCastClient client = new TypeCastClient(pongEndPoint); 
	    	PongService pongService = (PongService)client.getTypeCastInvocationHandler(PongService.class);
	    	pongService.pong(NetUtil.getLocalHost(), value);     	
    	} catch(Throwable t) { 
    		t.printStackTrace(); 
    	}
    }
    
    public Future<String> pingPong(String value) { 
    	try { 
	    	System.out.println("PingPong " + value); 
	    	return new Fact<String>(value); 
    	} catch(Throwable t) { 
    		t.printStackTrace(); 
    		throw new RuntimeException(t); 
    	} 	
    }
    public static void main(String[] args) { 
        try { 
            TypeCastServer server = new TypeCastServer(); 
            server.export(new PingServer()); 
        } catch(Throwable t) { 
            t.printStackTrace(); 
        }
    }

}
